package api

import (
	"net/http"

	"gitee.com/LangHuaHuachihua/blog-go/apps/user"
	"gitee.com/LangHuaHuachihua/blog-go/common"
	"github.com/gin-gonic/gin"
)

func (h *Handler) Login(c *gin.Context) {
	in := user.NewLoginUserRequest()
	//直接将数据转为json->到对象上
	err := c.BindJSON(in)
	if err != nil {
		common.NewResponse(c, http.StatusBadRequest, http.StatusBadRequest, err.Error())
	}
	ins, err := h.svc.Login(c.Request.Context(), in)
	if err != nil {
		resp := common.NewResponse(c, http.StatusInternalServerError, http.StatusInternalServerError, err.Error())
		c.JSON(http.StatusBadRequest, resp)
		// logger.L().Debug().Msg(err.Error())
		return
	}
	//为浏览器设置session存入token
	c.SetCookie(user.AUTH_COOKIE_NAME, ins.AccessToken, 3600*12, "/", "localhost", false, false)
	c.JSON(http.StatusOK, ins)
}
